package com.nondo.rdp.finance.endpoint.api.admin.setting.advert;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.rdp.auth.common.RoleUtils;
import com.nondo.rdp.auth.endpoint.model.Page;
import com.nondo.rdp.auth.endpoint.model.Query;
import com.nondo.rdp.core.common.BeanUtils;
import com.nondo.rdp.core.common.ValidateUtil;
import com.nondo.rdp.core.domain.attach.Attach;
import com.nondo.rdp.core.model.View;
import com.nondo.rdp.finance.domain.setting.advert.Advert;
import com.nondo.rdp.finance.service.setting.advert.AdvertService;

@RestController("apiAdminAdvert")
@RequestMapping("/api/admin/poster")
public class AdvertEndpoint {
    @Autowired
    AdvertService advertService;
    
    /**
     * 查看广告
     * @param request
     * @param response
     * @param id
     * @return
     */
    @RequestMapping("/info/{id}")
	public View info(HttpServletRequest request, HttpServletResponse response,
			@PathVariable("id") Long id) {
		View<Advert> view = new View<Advert>(20000, "获取数据成功");
		
		Advert advert = null;
		if (id != null) {
			advert = advertService.findOne(id);
		} else {
			advert = new Advert();
		}

		view.setData(advert);
		return view;
	}
    
    /**
     * 添加广告
     * @param request
     * @param response
     * @param id
     * @return
     */
    @PreAuthorize("hasRole('ROLE_" + RoleUtils.ADMIN + "')")
	@RequestMapping("/add")
	public View add(HttpServletRequest request, HttpServletResponse response,
			@Validated @RequestBody Advert advert, BindingResult result) {
		View view = new View(20000, "添加成功");

		if (result.hasErrors()) {
			view.setCode(40000);
			view.setMessage(ValidateUtil.first(result.getAllErrors()));
		} else {
			this.advertService.save(advert);
		}
		return view;
	}

    /**
     * 更新广告
     * @param request
     * @param response
     * @param id
     * @return
     */
	@PreAuthorize("hasRole('ROLE_" + RoleUtils.ADMIN + "')")
	@RequestMapping("/update")
	public View update(HttpServletRequest request,
			HttpServletResponse response,
			@Validated @RequestBody Advert advert, BindingResult result) {
		View view = new View(20000, "更新成功");

		if (result.hasErrors()) {
			view.setCode(40000);
			view.setMessage(ValidateUtil.first(result.getAllErrors()));
		} else {
			Advert advertDb = advertService.findOne(advert.getId());
			Attach imageDb = advertDb.getImage();
			Attach image = advert.getImage();
			image.setId(imageDb.getId());
			BeanUtils.copy(advert, advertDb);
			this.advertService.update(advertDb);
		}

		return view;
	}

    /**
     * 获取广告列表
     * @param request
     * @param response
     * @param query
     * @return
     */
	@PreAuthorize("hasRole('ROLE_" + RoleUtils.ADMIN + "')")
    @RequestMapping("/list")
    public View<Page> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<Advert> query) {
        View<Page> view = new View(20000, "获取列表成功");

        Advert advert = query.getData();

        Page<Advert> page = advertService.findAll(new Page<Advert>(query.getPage(), query.getSize()), advert);

        view.setData(page);

        return view;
    }

    /**
     * 删除广告
     * @param request
     * @param response
     * @param id
     * @return
     */
    @PreAuthorize("hasRole('ROLE_" + RoleUtils.ADMIN + "')")
    @RequestMapping("/delete/{id}")
    public View delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Long id) {
        View view = new View(20000, "删除成功");

        this.advertService.delete(id);

        return view;
    }
}